

/*

Figure 1

*/





local xcontrols 			= "  "
local xcontrolsrdd 		= " rdist*   "


local nametable =  "tables/"




forvalues xgraph = 1(1)3{
	
	
use "public_data/g30.dta", clear

	
		
gen hign 	= 0
gen lown 	= 1
gen higc = " "
gen lowc = " "
				
foreach crop_areat of varlist *_areat{
		
			capture drop inter*

			*defines a variable that measures whether crop is cultivated
			qui bys groupfe: egen interarea = max(`crop_areat')
			
			local crop 		= subinstr("`crop_areat'","_areat","",.)
			if "`crop'"=="cassavayam"{
				local crop = "cassava"
			}
			if "`crop'"== "cocoacoffeetea"{
				local crop = "cocoa"
			}
			if "`crop'"== "wspotato"{
				local crop = "spotato"
			}
			if "`crop'"== "bancocon"{
			local crop = "coconut"
			}
			if "`crop'"== "vegetables"{
				local crop = "tomato"
			}
			if "`crop'"== "pulses"{
				local crop = "phaseolus"
			}
			if "`crop'"== "cerealoth"{
				local crop = "barley"
			}
			local crop_null = "`crop'"+"_null"
			
			
			gen intermed = `crop_null'
			qui replace hign  = intermed 		if intermed>=hign&interarea>0
			qui replace lown  = intermed 		if intermed<=lown&interarea>0
			capture drop inter*
		}

		*Computes probabilty that highest or lowest risk failure crop cultivated
		gen higg = 0
		gen lowg = 0
		
		foreach crop_areat of varlist *_areat{
		
			capture drop inter*
			*defines a variable that measures whether crop is cultivated
			qui bys groupfe: egen interarea = max(`crop_areat')

			local crop 		= subinstr("`crop_areat'","_areat","",.)
			if "`crop'"=="cassavayam"{
				local crop = "cassava"
			}
			if "`crop'"== "cocoacoffeetea"{
				local crop = "cocoa"
			}
			if "`crop'"== "wspotato"{
				local crop = "spotato"
			}
			if "`crop'"== "bancocon"{
			local crop = "coconut"
			}
			if "`crop'"== "vegetables"{
				local crop = "tomato"
			}
			if "`crop'"== "pulses"{
				local crop = "phaseolus"
			}
			if "`crop'"== "cerealoth"{
				local crop = "barley"
			}
			local crop_null = "`crop'"+"_null"
			qui replace higg  = 1 if `crop_null'==hign&`crop_areat'>0
			qui replace lowg  = 1 if `crop_null'==lown&`crop_areat'>0
			qui replace higc  = "`crop'" + higc if `crop_null'==hign&interarea>0
			qui replace lowc  = "`crop'" + higc if `crop_null'==lown&interarea>0
			capture drop inter*
		}

gen cropn 	  = higc if "`varg'"=="higg"
replace cropn = lowc if "`varg'"=="lowg"
		

replace weight=1
	
gen 		rdist = rdist1in+rdist1out
gen 		mbin = int(rdist1in/5)*5 if varofi==1
replace	mbin = int(rdist1out/5)*5 if varofi==0
gen interb = abs(mbin)
bys groupfe: egen interbmin = min(varofi)
bys groupfe: egen interbmax = max(varofi)
drop if interbmax==interbmin
drop interb*
	
	
if `xgraph'==1{		
	gen inter = lowg
	local fileeps =   "`nametable'" + "glowg"
	local namevar = "Share of cells where low-risk crop is grown"
}
if `xgraph'==2{		
	gen inter = higg
	local fileeps =  "`nametable'" + "ghigg"
	local namevar = "Share of cells where high-risk crop is grown"
}
if `xgraph'==3{		
	gen inter = careawall>0
	local fileeps =   "`nametable'" + "glanduse"
	local namevar = "Share of cells with farming land"
}
	
	
	
xi: reg inter  rdist1* rdist2* rdist3*  if rdist<0 [aw=weight],
predict interp1  	if rdist<0
predict interse1  	if rdist<0, stdp
xi: reg inter  rdist1* rdist2* rdist3*  if rdist>0 [aw=weight],
predict interp2		if rdist>0
predict interse2		if rdist>0, stdp
gen		interp = interp1 	if rdist<0
replace	interp = interp2 	if  rdist>0
gen 		interse = interse1 	if	rdist<0
replace	interse = interse2 	if	rdist>0
gen 		interpab = interp + 2*interse
gen		interpbe  = interp - 2*interse
replace inter = inter*weight
collapse (mean) inter* weight, by(mbin varofi)
replace inter = inter/weight
gen ychange = 0
egen intermin = min(inter)
egen intermax = max(inter)
egen internmin = min(_n)
egen internmax = max(_n)
gen bounds 		= int(intermin*10)/10 if _n==internmin
replace bounds 	= ceil(intermax*10)/10 if _n==internmax
	
	
#delimit ;
	twoway scatter  inter mbin, mcolor(black) msymbol(+)
	||line interp mbin 		if varofi==0, lcolor(gs13)  sort
	||line interp mbin 		if varofi==1, lcolor(gs13)  sort
	||line interpab mbin 		if varofi==0, lcolor(gs13) lpattern(dot) sort
	||line interpab mbin 		if varofi==1, lcolor(gs13) lpattern(dot) sort
	||line interpbe mbin 		if varofi==0, lcolor(gs13) lpattern(dot) sort
	||line interpbe mbin 		if varofi==1, lcolor(gs13) lpattern(dot) sort
	||line bounds ychange, 
	xtitle("Distance to border", height(5) size(small)) ytitle("`namevar'", height(10) size(small)) 
	legend(off)
	;
	#delimit cr
	graph2tex , epsfile("`fileeps'") ///
	caption() label()
		
}


		
